package cn.starsray.resource;

import cn.starsray.common.CommonPage;
import cn.starsray.common.Response;
import cn.starsray.constant.ResponseEnum;
import cn.starsray.entity.Employee;
import cn.starsray.mapper.EmployeeMapper;
import cn.starsray.repository.EmployeeRepository;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.eclipse.microprofile.openapi.annotations.OpenAPIDefinition;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.info.Contact;
import org.eclipse.microprofile.openapi.annotations.info.Info;
import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import java.util.List;
import java.util.stream.Collectors;

@ApplicationScoped
@Path("m/employee")
@OpenAPIDefinition(info = @Info(title = "Employee API",
        contact = @Contact(name = "starsray", url = "http://localhost:8080/q/swagger-ui/", email = "starsray@163.com"),
        version = "1.0"))
public class EmployeeMybatisResource {

    @Inject
    EmployeeRepository employeeRepository;
    @Inject
    EmployeeMapper employeeMapper;

    @GET
    @Path("selectById")
    @Operation(
            summary = "查询单个员工信息",
            description = "selectById"
    )
    public List<cn.starsray.entity.Employee> selectById(@QueryParam("empNo") @Parameter(name = "empNo", description = "工号") Integer empNo) {
        return employeeRepository.find("emp_no", empNo).stream().collect(Collectors.toList());
    }

    @GET
    @Path("selectPage")
    @Operation(
            summary = "查询员工信息",
            description = "selectById"
    )
    public Response<CommonPage<Employee>> selectPage(@QueryParam("current") long current, @QueryParam("size") long size) {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        return Response.succ(ResponseEnum.SUCC, CommonPage.getPage(employeeMapper.selectPage(new Page<>(current, size), queryWrapper)));
    }
}
